<?php
require_once 'libraries/functions.php';

$path = "../ptnhuan-web1/data/films.txt";
$full_path = realpath($path);
$films = mreadFile($path);
$rating = NULL;
$rating1 = NULL;
?>

<form method="POST">
    From : <input type="text" name="rating">.<br>
    To :   <input type="text" name="rating1">.<br>
    <input type="submit" name="submit" value="Search">
</form>

<?php if (isset($_POST['submit'])): ?>
    <?php $rating = (double) $_POST['rating']; ?>
    <?php $rating1 = (double) $_POST['rating1']; ?>
<?php endif; ?>
<?php foreach ($films as $item): ?>
    <?php
    $film = explode(";", $item);
    $item = trim($item);
    if (!empty($item)):
        ?>
        <?php if (empty($rating) && empty($rating1)): ?>
            <?php if (isset($film[0]) || isset($film[2]) || isset($film[3])): ?>
                <?php if (isset($_GET['uid'])): ?>
                    <?php if ((int) $_GET['uid'] == 0): ?>
                        <?php echo $film[0] ?>
                        <img src = '../ptnhuan-web1/public/images/films/<?php echo $film[1] ?>'> 
                        <?php echo $film[2] ?>
                        <?php echo $film[3] ?>
                        <a href='edit.php?if= <?php echo "$film[0]"; ?>'>Edit</a>
                        <br>                    
                    <?php else: ?>
                        <?php if ((int) $_GET['uid'] == (int) $film[4]): ?>
                            <?php echo $film[0] ?>
                            <img src = '../ptnhuan-web1/public/images/films/<?php echo $film[1] ?>'> 
                            <?php echo $film[2] ?>
                            <?php echo $film[3] ?>
                            <a href='edit.php?if=<?php echo "$film[0]"; ?>'>Edit</a>
                            <br>
                        <?php endif; ?>
                    <?php endif; ?>  
                <?php endif; ?>
            <?php endif; ?>
        <?php else: ?>
            <?php if (isset($film[0]) || isset($film[2]) || isset($film[3])): ?>
                <?php if (isset($_GET['uid'])): ?>
                    <?php if ((int) $_GET['uid'] == 0): ?>
                        <?php if (($rating <= (double) $film[3]) && ($rating1 >= (double) $film[3])): ?>
                            <?php echo $film[0] ?>
                            <img src = '../ptnhuan-web1/public/images/films/<?php echo $film[1] ?>'> 
                            <?php echo $film[2] ?>
                            <?php echo $film[3] ?>
                            <a href='edit.php?if=<?php echo "$film[0]"; ?>'>Edit</a>
                            <br>    
                        <?php endif; ?>
                    <?php else: ?>
                        <?php if ((int) $_GET['uid'] == (int) $film[4]): ?>
                            <?php if (($rating <= (double) $film[3]) && ($rating1 >= (double) $film[3])): ?>
                                <?php echo $film[0] ?>
                                <img src = '../ptnhuan-web1/public/images/films/<?php echo $film[1] ?>'> 
                                <?php echo $film[2] ?>
                                <?php echo $film[3] ?>
                                <a href='edit.php?if=<?php echo "$film[0]"; ?>'>Edit</a>
                                <br>
                            <?php endif; ?>
                        <?php endif; ?>  
                    <?php endif; ?>
                <?php endif; ?>
            <?php endif; ?>
        <?php endif; ?> 
    <?php endif; ?>             
<?php endforeach; ?>
<?php echo '<a href="login.php">Bam vao day de quay lai<br></a>'; ?>
